<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/28
 * Time: 20:16
 */
declare(strict_types=1);

namespace App\Controller\MchApi;

use App\Exception\XZXValidationException;
use App\Service\DashboardService;
use App\Utils\DateUtils;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Middleware\MchApi\MchJWTAuthMiddleware;
use Hyperf\Contract\TranslatorInterface;

/**
 * Class DashboardController
 * @package App\Controller
 *
 * @Controller(prefix="/mch/api/dashboard")
 */
class DashboardController extends MchBaseController
{

    /**
     * @Inject
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;


    /**
     * @Inject()
     * @var DashboardService
     */
    protected $DashboardService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct(LoggerFactory $loggerFactory)
    {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    private function setLocale()
    {
        if($this->request->header('locale') == 'en')
        {
            $this->translator->setLocale('en');
        }
    }       

    /**
     * 实时统计
     * @PostMapping(path = "realTimeStats")
     * @Middleware(MchJWTAuthMiddleware::class)     
     */
    public function realTimeStats(RequestInterface $request)
    {
        $this->setlocale();
        $validator = $this->validationFactory->make(
            $request->all(),
            [
                // 'email' => 'required|email',
                // 'type'  => 'required|integer',
            ]
        );

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();
        $param['dailyStatsCreateTimeRange'][0] = DateUtils::dateToTimeStamp(date("Y-m-d 00:00:00"));
        $param['dailyStatsCreateTimeRange'][1] = $param['dailyStatsCreateTimeRange'][0]+86400;

        $param['monthlyStatsCreateTimeRange'][0] = DateUtils::dateToTimeStamp(date("Y-m-01 00:00:00"));
        $param['monthlyStatsCreateTimeRange'][1] = DateUtils::dateToTimeStamp(date("Y-m-01 00:00:00",strtotime("+1 month")));

        $ext = $this->request->getAttribute("ext");
        $param['mchId'] = $ext['mchId'];
        // 1.商户 2.平台
        $param['source'] = 1;
        return $this->DashboardService->realTimeStats($param);
    }

    /**
     * 折线图
     * @PostMapping(path = "lineChart")
     * @Middleware(MchJWTAuthMiddleware::class)     
     */
    public function lineChart(RequestInterface $request)
    {
        $this->setlocale();
        $validator = $this->validationFactory->make(
            $request->all(),
            [
                // 'email' => 'required|email',
                'dateType'  => 'required|integer',
                'lineType'  => 'required|integer',
            ]
        );

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();
        switch($param['dateType'])
        {
            case 1:
                //从昨天起最近7天
                $param['StatsTimeRange'][0] = DateUtils::dateToTimeStamp(date("Y-m-d 00:00:00",strtotime("-7 day")));
                $param['StatsTimeRange'][1] = DateUtils::dateToTimeStamp(date("Y-m-d 23:59:59",strtotime("-1 day")));
        
                break;
            case 2:
                //从昨天起最近一个月
                $param['StatsTimeRange'][0] = DateUtils::dateToTimeStamp(date("Y-m-d 00:00:00",strtotime("-1 month")));
                $param['StatsTimeRange'][1] = DateUtils::dateToTimeStamp(date("Y-m-d 23:59:59",strtotime("-1 day")));

                break;
            default:
                //从昨天起最近一年
                $param['StatsTimeRange'][0] = DateUtils::dateToTimeStamp(date("Y-m-d 00:00:00",strtotime("-1 year")));
                $param['StatsTimeRange'][1] = DateUtils::dateToTimeStamp(date("Y-m-d 23:59:59",strtotime("-1 day")));
                //最近一年
        }

        // $param['monthlyStatsCreateTimeRange'][0] = DateUtils::dateToTimeStamp(date("Y-m-01 00:00:00"));
        // $param['monthlyStatsCreateTimeRange'][1] = DateUtils::dateToTimeStamp(date("Y-m-01 00:00:00",strtotime("+1 month")));

        $ext = $this->request->getAttribute("ext");
        $param['mchId'] = $ext['mchId'];
        // 1.商户 2.平台
        $param['source'] = 1;
        return $this->DashboardService->lineChart($param);
    }



}